package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.sskj.cndragon.R;
import com.sskj.cndragon.component.DaggerUserDataComponent;
import com.sskj.cndragon.presenter.LoginActivityPresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.APP_LOGIN)
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {

    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.rl_see)
    CheckBox rlSee;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                MainActivity.start(this);
                finish();
            } else {
                userData = null;
            }
        });
        rlSee.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!b) {
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        });
        ClickUtil.click(300, tvRegister, () -> {
            RegisterActivity.start(this);
        });
        ClickUtil.click(300, tvForgetPwd, () -> {
            ForgetPwdActivity.start(this);
        });
        ClickUtil.click(300, btnLogin, () -> {
            if (isEmpty(etMobile)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurunindeshoujihao));
                return;
            }
            if (!CheckUtil.isMobile(getEtext(etMobile))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshuruzhengquedeshoujihao));
                return;
            }
            if (isEmpty(etPwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurudenglumima));
                return;
            }
            mPresenter.login(getEtext(etMobile), getEtext(etPwd));
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    public void loginSuccess(UserData userData) {
        if (userData != null) {
            SPUtil.put(SPConfig.ID, String.valueOf(userData.getId()));
            SPUtil.put(SPConfig.TOKEN, userData.getToken());
            userViewModel.insert(userData);
            HttpHeaders headers = new HttpHeaders();
            headers.put("token", userData.getToken());
            headers.put("systemType", "ANDROID");
//            HttpParams httpParams = new HttpParams();
//            httpParams.put("systemType", "ANDROID");
            OkGo.getInstance().addCommonHeaders(headers);
//            OkGo.getInstance().addCommonParams(httpParams);
            MainActivity.start(this);
            finish();
        }
    }
}
